﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using BrainTechLLC.WPF;
using DragDropLibrary;
using ImageAppDataModels;
using ImageAppViewModels;
using BrainTechLLC.ImageOperatorsNonAForgeDependent;
using BrainTechLLC.ImageOperators;
using BrainTechLLC.FolderExplorer;
using TemporalFilterExtensions;
using System.Drawing;
using System.Windows.Media;
using BrainTechLLC.FilterExtensions;
using System.Windows.Data;
using System.Collections.ObjectModel;
using System.Reflection;
using System.ComponentModel;

namespace WpfImageDuplicates
{
    public partial class ImageCollectionView : UserControl
    {
        public void DropOntoImageCollectionView(DropEventArgs<object> e)
        {
            if (e.DroppedOnto == this || e.DroppedOnto == lstItems)
            {
                if (DragAndDrop.GetRegisterDropTargetName(e.DroppedOnto) == "ThumbnailListView")
                {
                    if (DragAndDrop.GetRegisterDragSourceName(e.DragSource) == "ImageCollectionView")
                    {
                        // Just rearranging files                        
                        FileImageViewModel vmSource = e.Object as FileImageViewModel;
                        var item = this.InputHitTest(e.DropPoint);

                        System.Windows.Controls.Image img = item as System.Windows.Controls.Image;

                        if (img != null)
                        {
                            FileImageViewModel vmDest = img.DataContext as FileImageViewModel;

                            if (vmSource != vmDest)
                            {
                                var collection = Window1.MainWindow.FileImageCollectionViewModels[0]; // test
                                SwapItemsInImageCollectionView(vmSource, vmDest, collection);

                                var collection2 = Window1.MainWindow.CollectionViews[0].Displayed; // test
                                SwapItemsInImageCollectionView(vmSource, vmDest, collection2);
                            }
                        }
                    }
                    else if (DragAndDrop.GetRegisterDragSourceName(e.DragSource) == "FileListView" || DragAndDrop.GetRegisterDragSourceName(e.DragSource) == "SelectedFiles")
                    {
                        ShowFilesListView lv = BrainTechLLC.WPF.TempExten.FindParent<ShowFilesListView>(e.DragSource);

                        if (lv != null)
                        {
                            var fileInfos = lv.GetSelected();
                            for (int n = 0; n < fileInfos.Count; n++)
                            {
                                MoveFile(fileInfos[n].File, e);
                            }
                        }
                    }
                }
            }
        }

        private static void SwapItemsInImageCollectionView(FileImageViewModel vmSource, FileImageViewModel vmDest, ObservableCollection<FileImageViewModel> collection)
        {
            int indexSource = collection.IndexOf(vmSource);
            int indexDest = collection.IndexOf(vmDest);
            if (indexSource < indexDest)
            {
                collection.Move(indexDest, indexSource);
                collection.Move(indexSource + 1, indexDest);
            }
            else
            {
                collection.Move(indexSource, indexDest);
                collection.Move(indexDest + 1, indexSource);
            }
        }

        public void MoveFile(FileInfo fi, DropEventArgs<object> e)
        {
            if (fi != null && FileImageCollectionViewModel != null && FileImageCollectionViewModel.DirectoriesToExamine != null && FileImageCollectionViewModel.DirectoriesToExamine.Count > 0)
            {
                string oldFolderMatch = Path.GetDirectoryName(fi.FullName) + @"\";
                string newPath = fi.FullName.Replace(oldFolderMatch, FileImageCollectionViewModel.DirectoriesToExamine[0].Directory + @"\");
                try
                {
                    App.DataProvider.Move(fi.FullName, newPath);
                }
                catch { }

                DragAndDrop.RaiseDropSuccessful(e.DroppedOnto);
            }
        }

        private void lstItems_Drop(object sender, DragEventArgs e)
        {
            MessageBox.Show("listItems_Drop");
        }
    }
}